<!DOCTYPE html>
<html>

<head>
<meta charset='utf-8' />
<meta http-equiv="X-UA-Compatible" content="chrome=1" />
<meta name="description" content="Online ASN.1 Encoder powered by jsrsasign" />
<link rel="stylesheet" type="text/css" media="screen" href="../stylesheets/stylesheet.css">
<title>Online ASN.1 DER file generator (powered by jsrsasign)</title>
<script language="JavaScript" type="text/javascript" src="../jsrsasign-all-min.js"></script>
<script language="JavaScript" type="text/javascript" src="../test/jquery-1.4.2.min.js"></script>
<script language="JavaScript" type="text/javascript" src="../test/FileSaver.min.js"></script>
<script language="JavaScript" type="text/javascript">

var json_encoct1 = (function() {/*
{"seq": [
  {"octstr": {"obj": {"seq": [{"int": 3},{"int":4}]}}},
  {"prnstr": "aaa"}
  ]}
*/}).toString().match(/\/\*([^]*)\*\//)[1];

var json_eck1pub1 = (function() {/*
{"seq": [
  {"seq": [
    {"oid": {"oid": "1.2.840.10045.2.1"}},
    {"oid": {"oid": "1.2.840.10045.3.1.7"}}
    ]},
  {"bitstr": {"hex": "0004a01532a3c0900053de60fbefefcca58793301598d308b41e6f4e364e388c2711bef432c599148c94143d4ff46c2cb73e3e6a41d7eef23c047ea11e60667de425"}}
  ]}
*/}).toString().match(/\/\*([^]*)\*\//)[1];

var json_ecdsasig1 = (function() {/*
{"bitstr": 
  {"obj": 
    {"seq": [
      {"int": {"hex": "00df01c4bf58a06b3fa241824b10569a788ddab80b9dd8bb817936468c82d8401e"}},
      {"int": {"hex": "00d19175600fd50756f65ead8a0950c1da89b384f47bcf8d62ff917006f7558c27"}}
      ]}
    }
  }
*/}).toString().match(/\/\*([^]*)\*\//)[1];

var json_eedn1 = (function() {/*
{"seq": [
  {"set": [
      {"seq": [
        {"oid": {"oid": "2.5.4.6"}},
        {"prnstr": {"str": "EE"}}
        ]}
    ]},
  {"set": [
      {"seq": [
        {"oid": {"oid": "2.5.4.10"}},
        {"utf8str": {"str": "AS Sertifitseerimiskeskus"}}
        ]}
    ]},
  {"set": [
      {"seq": [
        {"oid": {"oid": "2.5.4.3"}},
        {"utf8str": {"str": "ESTEID-SK 2011"}}
        ]}
    ]},
  {"set": [
      {"seq": [
        {"oid": {"oid": "1.2.840.113549.1.9.1"}},
        {"ia5str": {"str": "pki@sk.ee"}}
        ]}
    ]}
  ]}
*/}).toString().match(/\/\*([^]*)\*\//)[1];

function _doEncode() {
  var f1 = document.form1; 
  var s = f1.s_in.value;
  try {
    var json = JSON.parse(s);
    var derObj = KJUR.asn1.ASN1Util.newObject(json);
    f1.s_out.value = derObj.getEncodedHex();
  } catch (ex) {
    alert("ERR: " + ex);
  }
}

function _doSave() {
  var f1 = document.form1; 
  var hex = f1.s_out.value;

  try {
    var isFileSaverSupported = !!new Blob;
  } catch (e) {
    alert("ERROR: FileSaver.js is not supported on your browser");
    return;
  }

  //var blob = new Blob([hex], {type: "text/plain;charset=utf-8"});
  //saveAs(blob, "asn1encode.hex.txt");

  var rstr = hextorstr(hex);
  var blob = new Blob([rstr], {type: "application/octet-stream"});
  saveAs(blob, "asn1encode.der");
}

function _setSample() {
  var f1 = document.form1;
  switch (f1.sample1.value) {
    case "encoct1": f1.s_in.value = json_encoct1; _doEncode(); break;
    case "eck1pub1": f1.s_in.value = json_eck1pub1; _doEncode(); break;
    case "ecdsasig1": f1.s_in.value = json_ecdsasig1; _doEncode(); break;
    case "eedn1": f1.s_in.value = json_eedn1; _doEncode(); break;
    default: f1.s_in.value = ""; break;
  }
}
</script>
</head>

<body>

<!-- HEADER -->
<div id="header_wrap" class="outer">
<header class="inner">
<h1 id="project_title">Online ASN.1 DER file generator</h1>
<h2 id="project_tagline">convert JSON represented ASN.1 structure to ASN.1 data</h2>
</header>
</div>

    <!-- MAIN CONTENT -->
    <div id="main_content_wrap" class="outer">
      <section id="main_content" class="inner">
<!-- now editing -->
<form name="form1">
<h4>(Step1) Fill ASN.1 structure by JSON string. The syntax is defined in <a href="https://kjur.github.io/jsrsasign/api/symbols/KJUR.asn1.ASN1Util.html#.newObject" target="_blank">ASN1Util.newObject</a>.</h4>

<textarea name="s_in" rows="10" cols="100">
{"seq": [
  {"octstr": {"obj": {"seq": [{"int": 3},{"int":4}]}}},
  {"bitstr": {"obj": {"seq": [{"int": 5},{"int":6}]}}},
  {"prnstr": "aaa"}
  ]}
</textarea>

<select name="sample1" onChange="_setSample()">
<option value="none"> You can also choose sample input here...
<option value="encoct1"> encapsulated OctetString sample
<option value="eck1pub1"> PKCS#8 ECC public key sample
<option value="ecdsasig1"> BitString encapsulated ECDSA signature value
<option value="eedn1"> distiguished name of Estonian e-Regident ID CA
</select>
<br/>

<input type="button" value="Encode" onClick="_doEncode();"/><br/>

<hr>
<h2>Result of ASN.1 encoded hexadecimal string:</h2>

<textarea name="s_out" cols="100" rows="10" readonly></textarea><br/>
<input type="button" value="Save this as local file" onClick="_doSave();"/><br/>
NOTE: Saving to local file is powered by 
<a href="https://github.com/eligrey/FileSaver.js" target="_blank">FileSaver.js</a>.
FileSaver.js supports most of modern browsers.

</form>
<!-- now editing -->

      </section>
    </div>

    <!-- FOOTER  -->
    <div id="footer_wrap" class="outer">
      <footer class="inner">
        <p class="copyright">jsjws maintained by <a href="https://github.com/kjur">@kjur</a></p>
        <p>Published with <a href="https://pages.github.com">GitHub Pages</a></p>
<div align="center" style="color: white">
Copyright &copy; 2016 Kenji Urushima. All rights reserved.
</div>
      </footer>
    </div>

  </body>
</html>
